﻿using System;
using System.Data.Entity;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using ITTestingSystem.BusinessLogic.DataServices.Abstract;
using ITTestingSystem.BusinessLogic.DataServices.Concrete;
using ITTestingSystem.BusinessLogic.PasswordHasher;
using ITTestingSystem.BusinessLogic.TokenService;
using ITTestingSystem.Controllers;
using ITTestingSystem.Domain;
using ITTestingSystem.Domain.Entities;
using ITTestingSystem.Extentions.Modules.AuthenticationProvider;
using ITTestingSystem.Extentions.Modules.EmailService;
using Ninject;

namespace ITTestingSystem.Extentions.Modules.ControllerFactory
{
    /// <summary>
    /// 
    /// </summary>
    /// 
    public class NinjectControllerFactory : DefaultControllerFactory
    {
        /// <summary>
        /// 
        /// </summary>
        /// 
        private readonly IKernel _kernel;

        /// <summary>
        /// 
        /// </summary>
        /// 
        public NinjectControllerFactory()
        {
            _kernel = new StandardKernel();
            AddBindings();
        }

        /// <summary>
        /// 
        /// </summary>
        /// 
        /// <param name="requestContext">
        /// 
        /// </param>
        /// 
        /// <param name="controllerType">
        /// 
        /// </param>
        /// 
        /// <returns>
        /// 
        /// </returns>
        /// 
        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            if (controllerType == null)
            {
                throw new HttpException(404, "Controller not found");
            }

            return _kernel.Get(controllerType) as IController;
        }

        /// <summary>
        /// 
        /// </summary>
        /// 
        private void AddBindings()
        {
            _kernel.Bind<IRepositoryForEntity<AuthenticationsDetail>>().To<RepositoryForEntity<AuthenticationsDetail>>();

            _kernel.Bind<IUserService>().To<UserService>();
            _kernel.Bind<IRepositoryForEntity<User>>().To<RepositoryForEntity<User>>();

            _kernel.Bind<ITestService>().To<TestService>();
            _kernel.Bind<IRepositoryForEntity<Test>>().To<RepositoryForEntity<Test>>();
            _kernel.Bind<IRepositoryForEntity<QuestionType>>().To<RepositoryForEntity<QuestionType>>();

            _kernel.Bind<IAuthenticationProvider>().To<AuthenticationProvider.AuthenticationProvider>();

            _kernel.Bind<IMailService>().To<MailService>();
            _kernel.Bind<MailController>().To<MailController>();

            _kernel.Bind<IPasswordHasher>().To<PasswordHasher>();

            _kernel.Bind<IUserTokenService>().To<UserTokenService>();

            _kernel.Bind<DbContext>().To<DomainContext>();
        }
    }
}